/*
 * Copyright 2014 Semidata. All rights reserved.
 */
package com.semidata.rtd.core.tcp.client;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @author shewei@semidata.com, She Wei
 * 
 */
public class TcpClientHandler extends SimpleChannelInboundHandler<Object> {

	private TcpResponseHandler handler;

	public TcpClientHandler(TcpResponseHandler handler) {
		this.handler = handler;
	}

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, Object msg)
	        throws Exception {
		if (msg instanceof ByteBuf) {
			ByteBuf buffer = (ByteBuf) msg;
			byte[] bytes = new byte[buffer.readableBytes()];
			buffer.readBytes(bytes);
			handler.handle(bytes);
		}
	}

}
